<?php
// +----------------------------------------------------------------------
// | UBTEN.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace User\Controller;
use Think\Controller;

class RegisterController extends Controller {
    
    
    public function mobile(){
        if(IS_POST){
            $user_code = I('user_code');
            $mobile_sms = I('mobile_sms');
            $session_sms = session('mobile_sms');
            $re = $this->check_verify($user_code);
            if($re){
                if($mobile_sms == $session_sms){
                    session('mobile_sms',null);
                    $datas['user_mobile'] = I('post.user_mobile');
                    $datas['user_passwd'] = I('post.user_passwd');
                    $password = password($datas['user_passwd']);
                    $datas['user_passwd'] = $password[0];
                    $datas['user_salt'] = $password[1];
                    $datas['user_time'] = time();
                    $datas['user_name'] = $datas['user_mobile'];
                    $datas['user_mobile_bind'] = 1;
                    $users = M('Users');
                    $tf = $users->data($datas)->add();
                    if($tf){
                        $res = $users->where(array('user_mobile'=>$datas['user_mobile']))->find();
                        session('user_id',$res['user_id']);
                        session('user_name',$res['user_name']);
                        $this->success('注册成功！', U('Index/index'));
                    }else{
                        $this->error('注册失败,请稍后重试！！！');
                    }
                }else{
                   $this->error('手机验证码不正确！'); 
                }
            }else{
                $this->error('图片验证码不正确！');
            }
        }
        $this->display('Login/mobile');
    }
    
    
    public function email(){
        if(IS_POST){
            $user_code = I('user_code');
            $email_sms = I('email_sms');
            $session_sms = session('email_sms');
            $re = $this->check_verify($user_code);
            if($re){
                if($email_sms == $session_sms){
                    $datas['user_email'] = I('post.user_email');
                    $datas['user_passwd'] = I('post.user_passwd');
                    $password = password($datas['user_passwd']);
                    $datas['user_passwd'] = $password[0];
                    $datas['user_salt'] = $password[1];
                    $datas['user_time'] = time();
                    $datas['user_name'] = $datas['user_email'];
                    $datas['user_email_bind'] = 1;
                    $users = M('Users');
                    $tf = $users->data($datas)->add();
                    if($tf){
                        $res = $users->where(array('user_email'=>$datas['user_email']))->find();
                        session('user_id',$res['user_id']);
                        session('user_name',$res['user_name']);
                        $this->success('注册成功！', U('Index/index'));
                    }else{
                        $this->error('因为某些原因,你的资料消失在黑洞中,注册失败!!!');
                    }
                }else{
                   $this->error('邮箱证码不正确！'); 
                }
            }else{
                $this->error('图片验证码不正确！');
            }
        }
        $this->display('Login/email');
    }
    
    
    public function forget(){
        if(IS_POST){
            $captcha = I('post.captcha');
            $u_name = I('post.u_name');
            $res = $this->check_verify($captcha);
            if($res){
                //判断用户输入的是用户名，或手机号，或邮箱
                $username = trim($u_name);
		        $userType = userLoginType($username);
		        if ($userType == 1) {
		            $map['user_email'] = array('eq', $username); //邮箱登陆
                            session("is_email",1);
		        } else if ($userType == 2) {
		            $map['user_mobile'] = array('eq', $username); //手机号登陆
                            session("is_mobile",1);
		        } else {
		            $map['user_name'] = array('eq', $username); //用户名登陆
		        }
                
//              if(strstr($u_name,'@')){
//                  $datas['user_email']        = $u_name;
//              }elseif(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",$u_name)){
//                  $datas['user_mobile'] = $u_name;
//              }else{
//                  $datas['user_name']    = $u_name;
//              }
                $users = M('Users');
                $tf = $users->where($map)->find();
                if($tf){
                    session("old_user_id",$tf['user_id']);
                    $this->success('操作成功',U('validation'));
                }else{
                    $this->error('该用户不存在!');
                }
            }else{
                $this->error('验证码错误');
            }
        }
        $this->display();
    }
    
        // 验证验证码
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }
}
